# from aip import AipFace
# import base64
#
# def face_rg(file_Path):
#     """ 你的 APPID AK SK """
#     APP_ID = '28400659'
#     API_KEY = 'R0QlKMEQux4XEHYRHAPiELPN'
#     SECRET_KEY = 'CtMxGBVpC6NilebQwCzNC6B9FABSbm5g'
#
#     client = AipFace(APP_ID, API_KEY, SECRET_KEY)
#
#     with open(file_Path, 'rb') as file:
#         data = base64.b64encode(file.read())  # 图片类型BASE64：图片的base64值：base64编码后的图片数据
#
#     image = data.decode()  # 解码
#
#     image_type = 'BASE64'
#     options = {}
#
#     options["face_field"] = "beauty"
#     options["max_face_num"] = 2
#     options["face_type"] = "LIVE"
#     options["liveness_control"] = "LOW"
#
#     """ 调用人脸检测 """
#     result = client.detect(image, image_type, options)
#     # print(result['result']['face_list'][0]['beauty'])
#     return result['result']['face_list'][0]['beauty']
#
#
# if __name__ == '__main__':
#     face_rg(fr"D:\py\16 爬虫2\212 虎牙颜值排行\img\eStar-眼里有你.jpg")

import requests
import parsel
from lxml import etree



url = 'https://www.huya.com/g/4079'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}
resp = requests.get(url=url, headers=headers)

et = etree.HTML(resp.text)
data_list = et.xpath('//*[@id="js-live-list"]/li')

for item in data_list:
    # item = parsel.Selector(item)
    img_url = item.xpath('./a[1]/img/@data-original')
    name = item.xpath('./span/span[1]/i/text()')
    # print(name, img_url)
    img_url = img_url[0].split('?')[0]

    img_resp = requests.get(url=img_url, headers=headers).content

    # 保存数据
    with open(f'img/{name[0]}.jpg', mode='wb') as f:
        f.write(img_resp)
    print(f'{name[0]}保存成功！')
